package com.ak.kaoyan.controller;


import com.ak.kaoyan.entity.Judge_Info;
import com.ak.kaoyan.service.impl.JudgeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Slf4j
@Controller
public class JudgeController {

    @Autowired
    JudgeServiceImpl judgeService;

    private static final String defaultSource = "public class HelloWorld {\n"
            + "    public static void main(String[] args) {\n"
            + "        System.out.println(\"Hello World!\");\n"
            + "    }\n"
            + "}";

    @RequestMapping(path = {"/"},method = RequestMethod.GET)
    public String entry(Model model){
        model.addAttribute("lastSource",defaultSource);
        return "ide";
    }

    @RequestMapping(path = {"/run"},method = RequestMethod.POST)
    public String runCode(@RequestParam("source") String source, @RequestParam("systemIn") String systemIn, Model model){
        log.info("代码为--"+source);
        log.info("输入为--"+systemIn);
//        String runResult = executeStringSourceService.execute(source,systemIn);
//        runResult = runResult.replaceAll(System.lineSeparator(),"<br/>");
        // 默认为java
        Judge_Info judge_info=judgeService.getResult(systemIn,source,"java");
        model.addAttribute("lastSource",source);
        model.addAttribute("lastSystemIn",systemIn);
        model.addAttribute("runResult",judge_info);
        return "ide";
    }


}
